import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

Vue.use(Router)
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { menuId: 1 },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', icon: 'dashboard', menuId: 'M_0' }
      }
    ]
  },
  {
    path: '/account',
    component: Layout,
    redirect: '/account',
    hidden: true,
    children: [
      {
        path: 'profile',
        name: 'profile',
        component: () => import('@/views/user/profile/account-profile')
      }
    ]
  }
]

export const asyncRoutes = [
]

export const childrenRoutes = []

const createRouter = () => {
  if (process.env.NODE_ENV === 'development') {
    constantRoutes.push({
      path: '/login',
      component: () => import('@/views/login/index'),
      hidden: true
    })
  }
  return new Router({
    // mode: 'history',  // require service support
    scrollBehavior: () => ({ y: 0 }),
    base: '/operator/',
    routes: constantRoutes
  })
}

const router = createRouter()

// console.log(router)

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  store.dispatch('permission/resetRoutes')
  router.matcher = newRouter.matcher // reset router
}

export default router
